 
<%@page import="org.apache.commons.lang.StringUtils"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	request.setAttribute("path", path);
	String pk = request.getParameter("pk");
		if(StringUtils.isEmpty(pk)){
		pk = "";
	}
%>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="Mosaddek">
    <meta name="keyword" content="">
    <link rel="shortcut icon" href="img/favicon.png">

    <title>
    <%
			if (StringUtils.isEmpty(pk)) {
				out.print( "新增菜单信息" );
			} else {
				out.print( "修改菜单信息" );
			}
		%>
	</title>
 
    <link href="${path}/style/def/css/bootstrap.min.css" rel="stylesheet">
    <link href="${path}/style/def/css/bootstrap-reset.css" rel="stylesheet">
    <!--external css-->
    <link href="${path}/style/authory/css/font-awesome.css" rel="stylesheet" />
    <!-- Custom styles for this template -->
    <link href="${path}/style/def/css/style.css" rel="stylesheet">
    <link href="${path}/style/def/css/style-responsive.css" rel="stylesheet" />


    <!-- HTML5 shim and Respond.js IE8 support of HTML5 tooltipss and media queries -->
    <!--[if lt IE 9]>
      <script src="${path}js/html5shiv.js"></script>
      <script src="${path}js/respond.min.js"></script>
    <![endif]-->
  </head>

  <body>
      <!--main content start-->
          <section class="wrapper">
              <!-- page start-->
              <div class="row">
                  <div class="col-lg-12">
                      <section class="panel">
                          <header class="panel-heading">
                            	 <%
									if (StringUtils.isEmpty(pk)) {
										out.print( "新增菜单信息" );
									} else {
										out.print( "修改菜单信息" );
									}
								%>
                          </header>
                          <div class="panel-body">
                              <div class="form">
                                  <form class="cmxform form-horizontal tasi-form" id="cResourceForm" method="post" action="">
                                      <input id="id" name="id" type="hidden"/>
                                  	   <div class="form-group col-lg-6">
                                          <label for="firstname" class="control-label col-sm-2">菜单名：</label>
											  <div class="col-sm-10">
											  	 <input class=" form-control" id="name" name="name" type="text"/>
											  </div>
                                      </div>
                                  	   <div class="form-group col-lg-6">
                                          <label for="firstname" class="control-label col-sm-2">路径：</label>
											  <div class="col-sm-10">
											  	 <input class=" form-control" id="url" name="url" type="text"/>
											  </div>
                                      </div>
                                  	   <div class="form-group col-lg-6">
                                          <label for="firstname" class="control-label col-sm-2">备注：</label>
											  <div class="col-sm-10">
											  	 <input class=" form-control" id="remark" name="remark" type="text"/>
											  </div>
                                      </div>
                                  	   <div class="form-group col-lg-6">
                                          <label for="parentId" class="control-label col-sm-2">父菜单：</label>
											  <div class="col-sm-10" id="parentId-comp-items">
											  </div>
                                      </div>
                                  	   <div class="form-group col-lg-6">
                                          <label for="moduleFlag" class="control-label col-sm-2">模块标签：</label>
											  <div class="col-sm-10">
											  	 <input class=" form-control" id="moduleFlag" name="moduleFlag" type="text"/>
											  </div>
                                      </div>
                                  	   <div class="form-group col-lg-6">
                                          <label for="firstname" class="control-label col-sm-2">序号：</label>
											  <div class="col-sm-10">
											  	 <input class=" form-control" id="sortNo" name="sortNo" type="text"/>
											  </div>
                                      </div>
                                      <div class="form-group">
                                          <div class="col-lg-offset-2 col-lg-10">
                                              <button class="btn btn-danger" type="button" onclick="submitForm()">保存</button>
                                              <button class="btn btn-default" type="button" onclick="javascript:window.location.href='${path}/jsp/menu/menu_list.jsp'">取消</button>
                                          </div>
                                      </div>
                                  </form>
                              </div>
                          </div>
                      </section>
                  </div>
              </div>
              <!-- page end-->
          </section>
      <!--main content end-->
     

      <!-- js placed at the end of the document so the pages load faster -->
    <script src="${path}/style/authory/js/jquery.js"></script>
    <script src="${path}/style/def/js/bootstrap.min.js"></script>
    <script class="include" type="text/javascript" src="${path}//style/authory/js/jquery.dcjqaccordion.2.7.js"></script>
    <script src="${path}/style/authory/js/jquery.scrollTo.min.js"></script>
    <script src="${path}/style/authory/js/bootbox.min.js"></script>
    <script src="${path}//style/def/js/jquery.nicescroll.js" type="text/javascript"></script>
    <script type="text/javascript" src="${path}/style/def/js/jquery.validate.min.js"></script>
     <script type="text/javascript" src="${path}/style/def/js/bootstrap-datetimepicker/js/bootstrap-datetimepicker.js"></script>
    <script src="${path}/style/def/js/respond.min.js" ></script>
	<script src="${path}/style/authory/js/common.js"></script>
    <!--common script for all pages-->
    <script src="${path}/style/authory/js/common-scripts.js"></script>

    <!--script for this page-->


	<script type="text/javascript">
	var pk = '<%=pk%>';
	$.validator.setDefaults({
        submitHandler: function(form) { form.submit(); }
    });

    // validate signup form on keyup and submit
    $("#cResourceForm").validate({
        rules: {
        	name: {
                required: true,
                rangelength:[2,10],
                remote: {
  					url: "${path}//isNameExit",
  					dataType: "json",
  					data: {   
  						id: function() {
  							return $("#id").val();
  						},
  						parentId: function() {
  							return $("#parentId").val();
  						},
  						name: function() {
  							return $("#name").val();
  						}
      				}
  				}
            },
            url: {
            	required: true,
                maxlength: 255
            },
            remark: {
                maxlength: 200
            },
            moduleFlag: {
            	required: function(){
            		return $("#parentId").val()=="";
            	},
                maxlength: 20
            },
            sortNo: {
            	required: true,
            	digits: true
            }
        },
        messages: {
        	name: {
                required: "请输入菜单名",
                rangelength: "菜单名的长度必须介于{0}和{1}",
                remote: "父菜单或者同个子菜单不能相同的菜单名"
            },
            url: {
            	required: "请输入路径",
                maxlength: "路径的最大长度为{0}"
            },
            remark: {
                maxlength: "菜单备注的最大长度为{0}"
            },
            moduleFlag: {
            	required: "父菜单必须输入标签",
                maxlength: "菜单标签的最大长度为{0}"
            },
            sortNo: {
            	required: "请输入序号",
            	digits: "序号必须为正整数"
            }
        }
    });
	var parentRes;
	jQuery.ajax({
		url : "${path}/rest/cResource/getParentResOpts",
		type : "get",
		success : function(result) {
			parentRes = result;
			jQuery.component({
				parentId:{
		 			 renderTo:"parentId-comp-items",
		 			 type:1,//1:下拉框 2:单选 3:多选
		 			 data:result
		 		 }
	      });
			$("#parentId").change(function(){
		    	var value = $("#parentId").val();
		    	if(value==""){
		    		$("#moduleFlag").attr("readOnly",false);
		    		$("#moduleFlag").val("");
		    	}else{
		    		for(var i=0;i<parentRes.length;i++){
		    			if(parentRes[i].code==value){
		    				$("#moduleFlag").val(parentRes[i].flag);
		    				break;
		    			}
		    		}
		    		$("#moduleFlag").attr("readOnly",true);
		    	}
		    });
		},
		error : function(result) {
			alert("获取菜单数据失败！");
		}

	});
      
	
	function gotoList(){
		window.location.href = "${path}/jsp/config/cResource_list.jsp";
	}
	
	function insertCResource(){
		jQuery.ajax({
			url : "${path}/rest/cResource/insertCResource",
			type : "get",
			data : $("#cResourceForm").serialize(),
			success : function(result) {
				if (result == "success") {
					bootbox.alert({message:"保存成功！",callback:function(){
						gotoList();
  					}});
				} else {
					bootbox.alert( '保存失败'); 
				}
			},
			error : function(result) {
				bootbox.alert('保存失败'); 
			}

		});
	}
	
	function updateCResource(){
		jQuery.ajax({
			url : "${path}/rest/cResource/updateCResource",
			type : "post",
			data : $("#cResourceForm").serialize(),
			success : function(result) {
				if (result == 'success') {
					bootbox.alert({message:"保存成功！",callback:function(){
						gotoList();
  					}});
				} else {
					bootbox.alert('保存失败'); 
				}
			},
			error : function(result) {
				bootbox.alert('保存失败'); 
			}
		});
	}
	
	function submitForm(){
		if (pk == "") {
			$("#cResourceForm").attr("action", "${path}/rest/cResource/insertCResourceJsp");
			$("#cResourceForm").submit();
		} else {
			$("#cResourceForm").attr("action", "${path}/rest/cResource/updateCResourceJsp");
			$("#cResourceForm").submit();
		}
	}
	
	function ajaxSubmitForm(){
		if (pk == "") {
			insertCResource();
		} else {
			updateCResource();
		}
	}
	
	$(function(){
		if(pk!=''){
			jQuery.ajax({
				url : "${path}/rest/cResource/getDetailByPk",
				type : "post",
				data : {pk:pk},
				success : function(result) {
					if(result=='undefined'){
						return;
					}
					$("input[name='id']").val(result.id);
					$("input[name='name']").val(result.name);
					$("input[name='url']").val(result.url);
					$("input[name='remark']").val(result.remark);
					$("input[name='moduleFlag']").val(result.moduleFlag);
					$("select[name='parentId']").val(result.parentId);
					$("input[name='sortNo']").val(result.sortNo);
					$("input[name='moduleFlag']").val(result.moduleFlag);
					if(result.parentId){
						$("#moduleFlag").attr("readOnly",true);
					}
				},
				error : function(result) {
					
				}
			});
		}
	});
	
	</script>
  </body>
</html>
